package com.ys.controller.web.hr;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.hr.ThrTransparencyProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.hr.ThrTransparencyProgram;
import com.ys.entity.hr.ThrTransparencyProgramItem;
import com.ys.entity.sys.YsProcInst;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/hr/thrTransparencyProgram")
public class WebThrTransparencyProgramController {


	@Autowired
	private CommonApiService<ThrTransparencyProgram> commonApiService;


	@Autowired
	private ThrTransparencyProgramService thrTransparencyProgramService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.THR_TransparencyProgram);
			ThrTransparencyProgram thrTransparencyProgram = new ThrTransparencyProgram()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.THR_TransparencyProgram, new ArrayList<Map<String, Object>>());

			return commonApiService.toAdd(model, thrTransparencyProgram, MenuId.THR_TransparencyProgram, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("bid") == null){
			return W.error(model, "单号不能为空");
		}

		String bid = (params.get("bid").toString());

		ThrTransparencyProgram entity = new ThrTransparencyProgram().setBid(bid).queryFirst();
		if (entity == null){
			return W.error(model, "透明计划申报单不存在!");
		}

		YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("updateTime1", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));


		try {
			List<Map<String, Object>> sonlist = thrTransparencyProgramService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.THR_TransparencyProgram, sonlist);

			return commonApiService.toEdit(model, entity.getId(), new ThrTransparencyProgram(), MenuId.THR_TransparencyProgram, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}

	}

}
